#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <functional>


using func_t = std::function<void(const std::string&)>;

class Thread
{
public:
    Thread(std::string name, func_t func) :_name(name), _func(func), _isrunning(0)
    { }
    ~Thread()
    { }

    void Excute()
    {
        _isrunning = 1;
        _func(_name);
        _isrunning = 0;
    }

    static void* ThreadRoutine(void* args)
    {
        Thread* t = static_cast<Thread*>(args);
        t->Excute();
        return nullptr;
    }

    bool start()
    {
        int ret = pthread_create(&_tid, nullptr, ThreadRoutine, this);
        return ret == 0;
    }

    void stop()
    {
        if(_isrunning)
        {
            ::pthread_cancel(_tid);
            _isrunning = 0;
        }
    }

    void join()
    {
        pthread_join(_tid, nullptr);
    }
private:
    std::string _name;
    pthread_t _tid;
    func_t _func;
    bool _isrunning;
};